package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.ContractDao;
import cn.itcast.dao.cargo.InvoiceDao;
import cn.itcast.dao.cargo.PackingDao;
import cn.itcast.dao.cargo.ShippingDao;
import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.InvoiceService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Service
public class InvoiceServiceImpl implements InvoiceService {

    @Autowired
    private InvoiceDao invoiceDao;

    @Autowired
    private PackingDao packingDao;

    @Autowired
    private ContractDao contractDao;

    @Autowired
    private ShippingDao shippingDao;

    /**
     * 查：分页
     *
     * @param page
     * @param size
     * @param invoiceExample
     * @return
     */
    @Override
    public PageInfo findAll(int page, int size, InvoiceExample invoiceExample) {
        PageHelper.startPage(page, size);
        List<Invoice> list = invoiceDao.selectByExample(invoiceExample);
        for (Invoice invoice : list) {
            System.out.println(invoice.getCreateTime());
        }
        return new PageInfo(list);
    }

    @Override
    public Invoice findById(String id) {
        return invoiceDao.selectByPrimaryKey(id);
    }

    /**
     * 增：Invoice
     * bl_no 和 pobox 由页面上传
     *
     * @param invoice
     * @param shipping
     */
    @Override
    public void save(Invoice invoice, Shipping shipping) {
        String id = shipping.getId();

        //根据共用主键id查Packing
        PackingExample packingExample = new PackingExample();
        PackingExample.Criteria packingCriteria = packingExample.createCriteria();
        packingCriteria.andIdEqualTo(id);
        Packing packing = packingDao.selectByPrimaryKey(id);

        //发票的id为委托Shipping的id
        invoice.setId(id);
        //发票的buyer为装箱Packing的buyer
        invoice.setBuyer(packing.getBuyer());
        //发票的seller为装箱Packing的seller
        invoice.setSeller(packing.getSeller());
        //发票的sc_no为Packing的export_nos
        invoice.setScNo(packing.getExportNos());
        //发票的bl_no由页面用户输入

        //发票的trade_term为对应合同的trade_term
        String[] contractNos = packing.getExportNos().split(" ");
        String tradeTerms = "";
        for(int i = 0; i < contractNos.length; i++){
            /*Contract contract = contractDao.selectByPrimaryKey(contractIds[i]);*/
            Contract contract = contractDao.selectByNo(contractNos[i]);
            if(i < contractNos.length - 1) {
                tradeTerms += contract.getTradeTerms() + ",";
            }else if(i == contractNos.length - 1){
                tradeTerms += contract.getTradeTerms();
            }

            invoice.setTradeTerms(tradeTerms);
        }

        /*for (String contractId : contractIds) {
            String tradeTerms = "";
            Contract contract = contractDao.selectByPrimaryKey(contractId);
            tradeTerms += contract.getTradeTerms() + ",";
            invoice.setTradeTerms(tradeTerms);
        }*/

        //发票的pobox由页面用户输入
        //将发票状态改为草稿
        invoice.setState(0);
        //发票创建时间为当前时间
        invoice.setCreateTime(new Date());

        invoiceDao.insertSelective(invoice);

        //将委托单状态改为1 已出票
        shipping.setState(1);
        shippingDao.updateByPrimaryKeySelective(shipping);
    }

    @Override
    public void update(Invoice invoice) {

    }

    @Override
    public void delete(String id) {
        Invoice invoice = invoiceDao.selectByPrimaryKey(id);
        if (invoice.getState() == 0) {
            invoiceDao.deleteByPrimaryKey(id);
        }
    }

    @Override
    public List<Invoice> findAll(InvoiceExample example) {
        return invoiceDao.selectByExample(example);
    }
}